﻿using System.Data.Entity;

namespace com.Bliss.Domain.Post
{
    public class PostDbContext : DbContext
    {
        public PostDbContext()
            : base("BlissConString")
        {

        }

        public DbSet<Bliss.Post> Posts { get; set; }
        public DbSet<Bliss.User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Bliss.Post>()
               .HasOptional(p => p.PostedTo)
               .WithMany(c => c.PostsTo)
               .HasForeignKey(p => p.ToID)
               .WillCascadeOnDelete(false);

            modelBuilder.Entity<Bliss.Post>()
                .HasRequired(p => p.PostedBy)
                .WithMany(u => u.Posts)
                .HasForeignKey(p => p.FromId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Bliss.Post>()
                .HasOptional(p => p.Parent)
                .WithMany(c => c.Comments)
                .HasForeignKey(p => p.ParentPostID)
                .WillCascadeOnDelete(false);

        }
    }
}
